﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using WebCurriculumn.DBAcess;
using WebCurriculumn.Helps;
using System.Web.UI.WebControls;

namespace WebCurriculumn.Helper
{
    public class CommonFunction
    {
        public static void CopyData(DBContext targetDB, DBContext sourceDB, string spGetData, string tableName, string spInsertData, params object[] parameters)
        {
            SqlResult result = sourceDB.CallProcedure(spGetData, parameters);
            InserDataToTable(targetDB, result, tableName, spInsertData);
        }

        public static void InserDataToTable(DBContext DB, SqlResult result, string tableName, string spName)
        {
            if (result.DataSet.Tables.Count > 0 && result.DataSet.Tables[0].Rows.Count > 0)
            {
                DataTable dt = result.DataSet.Tables[0];
                //string sql = "select name from syscolumns where ID=object_id('" + tableName + "')";
                SqlParameter[] parameterList = DB.GetProcedureParameter(spName);
                int count = parameterList.Length;
                foreach (DataRow dr in dt.Rows)
                {
                    object[] param = new object[count];
                    for (int i = 0; i < parameterList.Length; i++)
                    {
                        if (parameterList[i].DbType == DbType.String)
                        {
                            param[i] = dr[i].ToString();
                        }
                        else if (parameterList[i].DbType == DbType.Int32)
                        {
                            param[i] = MyUtils.ParseToInt(dr[i].ToString(), 0);
                        }
                        else if (parameterList[i].DbType == DbType.DateTime)
                        {
                            param[i] = MyUtils.ParseToDateTime(dr[i].ToString(), DateTime.Now);
                        }
                        else if (parameterList[i].DbType == DbType.Double)
                        {
                            param[i] = MyUtils.PareToDouble(dr[i].ToString(), 0.0);
                        }
                    }
                    DB.CallProcedure(spName, param);
                }
            }
        }

        public static void BindCombox(DataTable dt, DropDownList cmb)
        {
            DataTable tempDt = new DataTable();
            tempDt.Columns.Add("value", typeof(String));
            tempDt.Columns.Add("text", typeof(String));
            foreach (DataRow dr in dt.Rows)
            {
                tempDt.Rows.Add(dr[0].ToString(), dr[1].ToString());
            }
            cmb.DataSource = tempDt;
            
            cmb.DataTextField = "text";
            cmb.DataValueField = "value";
            cmb.DataBind();
        }

        public static void BindComboxWithNoExistValue(DataTable dt, DropDownList cmb, List<string> teacherList)
        {
            DataTable tempDt = new DataTable();
            tempDt.Columns.Add("value", typeof(String));
            tempDt.Columns.Add("text", typeof(String));
            foreach (DataRow dr in dt.Rows)
            {
                if (!teacherList.Contains(dr[1].ToString()))
                {
                    tempDt.Rows.Add(dr[0].ToString(), dr[1].ToString());
                }
            }
            cmb.DataSource = tempDt;
            
            cmb.DataTextField = "text";
            cmb.DataValueField = "value";
            cmb.DataBind();
        }

        public static string GetInitValueByKey(string key)
        {
            return ConfigurationManager.AppSettings[key];
        }

        public static void BindStatusCombox(DropDownList cmb)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("value", typeof(String));
            dt.Columns.Add("text", typeof(String));
            dt.Rows.Add("0","");
            dt.Rows.Add(((int)(EumnCurriLumnStatus.未入力)).ToString(),EumnCurriLumnStatus.未入力);
            dt.Rows.Add(((int)(EumnCurriLumnStatus.入力中)).ToString(), EumnCurriLumnStatus.入力中);
            dt.Rows.Add(((int)(EumnCurriLumnStatus.教育指針印刷済)).ToString(), EumnCurriLumnStatus.教育指針印刷済);
            dt.Rows.Add(((int)(EumnCurriLumnStatus.教育指針承認)).ToString(), EumnCurriLumnStatus.教育指針承認);
            dt.Rows.Add(((int)(EumnCurriLumnStatus.ＳＢＯ印刷済)).ToString(), EumnCurriLumnStatus.ＳＢＯ印刷済);
            dt.Rows.Add(((int)(EumnCurriLumnStatus.ＳＢＯ承認)).ToString(), EumnCurriLumnStatus.ＳＢＯ承認);
            cmb.DataSource = dt;
            //cmb.DisplayMember = "text";
           // cmb.ValueMember = "value";
        }


        public static void UpdateStatus(object[] paras)
        {
            using (DBContext db = new DBContext())
            {
                db.CallProcedure("usp_tbstrCurriCulumnStatus_add_edit", paras);
            }
        }

        public static string WeekdayName(string  date)
        {
            DateTime nwdate = DateTime.Parse(date);
            //int day = int.Parse(nwdate.DayOfWeek.ToString());
            int day = Convert.ToInt32(nwdate.DayOfWeek);
            switch (day)
            {
                case 0:
                    return "日";
                case 1:
                    return "月";
                case 2:
                    return "火";
                case 3:
                    return "水";
                case 4:
                    return "木";
                case 5:
                    return "金";
                case 6:
                    return "土";
                default:
                    return "";
            }
        }
    }
}
